home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / copy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  5.0 KB  |  267 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    Copy.c
  5. *    Cut/copy/paste/clear
  6. *        
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *        
  15. **********************************************************************
  16. *    
  17. */ 
  18.  
  19. #include "EMBL-Search.h"
  20. #include "EMBL-Search.rsrc.h"
  21.  
  22. /*
  23. ******************************* Prototypes ***************************
  24. */
  25.  
  26. #include "copy.h"
  27. #include "util.h"
  28. #include "hitstorage.h"
  29. #include "sequence.h"
  30.  
  31. static void CopyResSelection(WDPtr wdp);
  32. static void CopySequence(WDPtr wdp);
  33.  
  34.  
  35.  
  36. /**************************************
  37. *    Cut dispatcher
  38. *    Return value:    none    
  39. */
  40.  
  41. void CutSelection(WDPtr wdp)
  42. {    
  43.     if( wdp == NULL )
  44.         return;
  45.         
  46.     switch( ((WindowPeek)wdp)->windowKind ) {
  47.         case queryW:
  48.             DlgCut((DialogPtr)wdp);
  49.             ZeroScrap();
  50.             TEToScrap();
  51.             wdp->dirty = TRUE;
  52.             break;
  53.         case seqW:
  54.             break;
  55.         case resW:
  56.             break;
  57.     }
  58. }
  59.  
  60.  
  61. /**************************************
  62. *    Clear dispatcher
  63. *    Return value:    none    
  64. */
  65.  
  66. void ClearSelection(WDPtr wdp)
  67. {    
  68.     if( wdp == NULL )
  69.         return;
  70.         
  71.     switch( ((WindowPeek)wdp)->windowKind ) {
  72.         case queryW:
  73.             DlgDelete((DialogPtr)wdp);
  74.             wdp->dirty = TRUE;
  75.             break;
  76.         case seqW:
  77.             break;
  78.         case resW:
  79.             break;
  80.     }
  81. }
  82.  
  83.  
  84. /**************************************
  85. *    Copy dispatcher
  86. *    Return value:    none    
  87. */
  88.  
  89. void CopySelection(WDPtr wdp)
  90. {    
  91.     if( wdp == NULL )
  92.         return;
  93.         
  94.     switch( ((WindowPeek)wdp)->windowKind ) {
  95.         case queryW:
  96.             DlgCopy((DialogPtr)wdp);
  97.             ZeroScrap();
  98.             TEToScrap();
  99.             break;
  100.         case seqW:
  101.             CopySequence(wdp);
  102.             break;
  103.         case resW:
  104.             CopyResSelection(wdp);
  105.             break;
  106.     }
  107. }
  108.  
  109. /**************************************
  110. *    Paste dispatcher
  111. *    Return value:    none    
  112. */
  113.  
  114. void PasteSelection(WDPtr wdp)
  115. {    
  116.     if( wdp == NULL )
  117.         return;
  118.         
  119.     switch( ((WindowPeek)wdp)->windowKind ) {
  120.         case queryW:
  121.             TEFromScrap();
  122.             DlgPaste((DialogPtr)wdp);
  123.             wdp->dirty = TRUE;
  124.             break;
  125.         case seqW:
  126.             break;
  127.         case resW:
  128.             break;
  129.     }
  130. }
  131.  
  132.  
  133. /**************************************
  134. *    Copy selection in result window
  135. *    Return value:    none    
  136. */
  137.  
  138. static void CopyResSelection(WDPtr wdp)
  139. {
  140.     ResultHdl        resHdl;
  141.     CString80Hdl    bufHdl;
  142.     HitlistHdl        hlHdl;
  143.     SignedByte        oldState;
  144.     short                hitPos,bufPos, found;
  145.     long                count;
  146.     char                *buf,*dest;
  147.     OSErr                ret;
  148.     Boolean            ret2;
  149.     
  150.     if( wdp == NULL || ((WindowPeek)wdp)->windowKind != resW )
  151.         return;
  152.  
  153.     resHdl = (ResultHdl)(wdp->userHandle);
  154.     bufHdl = (**resHdl).descBufHdl;
  155.     hlHdl     = (**resHdl).hlHdl;
  156.     oldState=LockHandleHigh((Handle)bufHdl);
  157.     
  158.     /* allocate a new memory block large enough to hold all selected lines */
  159.     count = (**resHdl).nsel * sizeof(CString80);
  160.     if( (buf = (char *)NewPtrClear(count)) == NULL) {
  161.         ErrorMsg(ERR_MEMORY);
  162.         goto err;
  163.     }
  164.     else {    /* clear buffer */
  165.         *buf=EOS;
  166.         dest=buf;
  167.     }
  168.      
  169.     /* now copy hit by hit */
  170.     for(    hitPos = 0, found = 0;
  171.             hitPos < (**resHdl).nhits && found < (**resHdl).nsel;
  172.             hitPos++, bufPos++) {
  173.  
  174.         /* copy to copy buffer */
  175.         if( GetSelectState(hlHdl,hitPos) ) {
  176.             /* refill result buffer if necessary */
  177.             if( hitPos < (**resHdl).buftop || hitPos>= (**resHdl).buftop + MAXBUFLINES) {
  178.                 StartWaitCursor();
  179.                 ret2 = FillDEBuffer(resHdl,hitPos,FALSE);
  180.                 InitCursor();
  181.                 if( !ret )
  182.                     goto err;
  183.             }
  184.         
  185.             found++;
  186.             BlockMove((char *)((*bufHdl)[hitPos - (**resHdl).buftop])+1,dest,80L);
  187.             dest += 80;
  188.            /* add newline */
  189.             *dest++ = '\r';
  190.         }
  191.     }
  192.     
  193.     ret=ZeroScrap();
  194.     /* Don't copy last \r */
  195.     ret=PutScrap(--count,'TEXT',buf);
  196.  
  197. err:
  198.     if(buf) DisposPtr((Ptr)buf);
  199.     HSetState((Handle)bufHdl,oldState);
  200. }
  201.  
  202.  
  203. /**************************************
  204. *    Copy sequence entry to clipboard
  205. *    Return value:    none    
  206. */
  207.  
  208. static void CopySequence(WDPtr wdp)
  209. {
  210.     CString80Hdl    bufHdl;
  211.     SeqRecHdl        seqRecHdl;
  212.     SignedByte        oldState;
  213.     short                hitPos,bufPos;
  214.     long                count;
  215.     char                *buf,*dest;
  216.     unsigned short linelen;
  217.     OSErr                ret;
  218.  
  219.     if( wdp == NULL || ((WindowPeek)wdp)->windowKind != seqW )
  220.         return;
  221.  
  222.     seqRecHdl = (SeqRecHdl)(wdp->userHandle);
  223.     if(seqRecHdl == NULL)
  224.         return;
  225.     
  226.     /* Allocate memory block large enough to hold complete entry */
  227.     if( (buf = (char *)NewPtrClear((**seqRecHdl).nlines * sizeof(CString80))) == NULL) {
  228.         ErrorMsg(ERR_MEMORY);
  229.         return;
  230.     }
  231.     else {    /* empty buffer */
  232.         *buf=EOS;
  233.         dest=buf;
  234.     }
  235.             
  236.     /* Lock down sequence data */
  237.     bufHdl = (**seqRecHdl).lineBufHdl;
  238.     oldState=LockHandleHigh((Handle)bufHdl);
  239.     
  240.     bufPos = 0;
  241.     count = 0L;
  242.     for(hitPos = (**seqRecHdl).firstSel; hitPos <= (**seqRecHdl).lastSel; hitPos++) {
  243.         if( hitPos < (**seqRecHdl).buftop || hitPos>= (**seqRecHdl).buftop + SEQBUFLINES ) {
  244.             if(!FillLineBuffer(seqRecHdl,hitPos))
  245.                 break;
  246.             else
  247.                 bufPos = 0;
  248.         }
  249.         
  250.         linelen = (unsigned short)*(*bufHdl)[bufPos];
  251.         BlockMove((char *)((*bufHdl)[bufPos])+1,dest,linelen);
  252.         dest += linelen;
  253.         count += linelen+1; /* +1 because of \r */
  254.        /* add newline */
  255.         *dest++ = '\r';
  256.         
  257.         bufPos++;
  258.     }
  259.  
  260.     ret=ZeroScrap();
  261.     /* Don't copy last \r */
  262.     ret=PutScrap(--count,'TEXT',buf);
  263.  
  264. err:
  265.     if(buf) DisposPtr((Ptr)buf);
  266.     HSetState((Handle)bufHdl,oldState);
  267. }